*******************************************************************************
* Oak Park
*******************************************************************************

use "dataset_other_oak_park", clear

gen B = .
gen SE = .
gen N = .

gen jurisdiction = "oak park, illinois"
gen design = "Cross-sectional"

replace pctyes = -pctyes + 1

su pctnhblack
replace pctnhblack = (pctnhblack - r(min)) / (r(max) - r(min))

reg pctyes pctnhblack [aw=totalvotes], robust

replace B = _b[pctnhblack]
replace SE = _se[pctnhblack]
replace N = e(N)

collapse B SE N, by(juris design)

save tmp, replace

*******************************************************************************
* Other Louisiana parishes
*******************************************************************************

use "dataset_other_louisiana_parishes", clear

gen B = .
gen SE = .
gen N = .

gen design = ""
gen jurisdiction = parish

levelsof parish, local(parishes)
foreach parish of local parishes {

	preserve
	
		keep if parish == "`parish'"
		
		su pctblack
		replace pctblack = (pctblack - r(min)) / (r(max) - r(min))
		
		xi: reg pctyes i.year*pctblack [aw=totalvotes], robust
		
		egen npreyearsm = nvals(year) if year < 2020
		egen npreyears = mean(npreyearsm)
		egen npostyearsm = nvals(year) if year >= 2020
		egen npostyears = mean(npostyearsm)
		
		su year if year >= 2020
		local firstpost = r(min)
		
		* parishes with no post years
		local B = .
		local SE = .
		local N = .
		
		* parishes with pre and post data
		if npostyears >= 1 & npreyears >= 1 & !mi(npostyears) & ///
			!mi(npreyears) {
				local B = _b[_IyeaXpct_`firstpost']
				local SE = _se[_IyeaXpct_`firstpost']
				local N = e(N)
				local design = "Panel"
		}
		
		* parishes with only post data
		if npostyears == 1 & npreyears == . {
			local B = _b[pctblack]
			local SE = _se[pctblack]
			local N = e(N)
			local design = "Cross-sectional"
		}
			
	restore

	replace B = `B' if parish == "`parish'"
	replace SE = `SE' if parish == "`parish'"	
	replace N = `N' if parish == "`parish'"
	replace design = "`design'" if parish == "`parish'"
		
}

collapse B SE N, by(juris design)

append using tmp
save tmp, replace

*******************************************************************************
* Other Texas cities
*******************************************************************************

use "dataset_other_texas_cities", clear

gen B = .
gen SE = .
gen N = .

gen design = "Panel"
gen jurisdiction = city

levelsof city, local(cities)
foreach city of local cities {

	preserve
	
		keep if city == "`city'"
		
		su pctnhblack
		replace pctnhblack = (pctnhblack - r(min)) / (r(max) - r(min))
		
		xi: reg pctyes i.year*pctnhblack [aw=totalvotes], robust
				
		su year if year >= 2020
		local firstpost = r(min)
		
		local B = .
		local SE = .
		local N = .
		
		su year if year >= 2020
		if r(min) != r(max) & r(min) !=. {
			local B = _b[_IyeaXpct_`firstpost']
			local SE = _se[_IyeaXpct_`firstpost']
			local N = e(N)
		}
		
		* account for city with one post-year
		su year if year >= 2020
		if r(min) == r(max) & r(min) !=. {
			local B = _b[pctnhblack]
			local SE = _se[pctnhblack]
			local N = e(N)
		}
			
	restore
	
	if "`city'" == "euless, texas" replace design = ///
		"Cross-sectional" if city == "`city'"
	replace B = `B' if city == "`city'"
	replace SE = `SE' if city == "`city'"	
	replace N = `N' if city == "`city'"	
		
}

collapse B SE N, by(juris design)

append using tmp
save tmp, replace

*******************************************************************************
* Main text results
*******************************************************************************

* EBR

use "dataset_east_baton_rouge_cross", replace

gen B = .
gen SE = .
gen N = .

gen jurisdiction = "EBR"
gen design = "Panel"

su pctnhblack
replace pctnhblack = (pctnhblack - r(min)) / (r(max) - r(min))

xi: reg pctyes i.year*pctnhblack [aw=totalvotes], robust

replace B = _b[_IyeaXpct_2020]
replace SE = _se[_IyeaXpct_2020]
replace N = e(N)

collapse B SE N, by(juris design)

append using tmp
save tmp, replace

* FW

use "dataset_fort_worth_cross", replace

gen B = .
gen SE = .
gen N = .

gen jurisdiction = "FW"
gen design = "Panel"

su pctnhblack
replace pctnhblack = (pctnhblack - r(min)) / (r(max) - r(min))

xi: reg pctyes i.year*pctnhblack [aw=totalvotes], robust

replace B = _b[_IyeaXpct_2020]
replace SE = _se[_IyeaXpct_2020]
replace N = e(N)

collapse B SE N, by(juris design)

*******************************************************************************
* Combine and graph
*******************************************************************************

append using tmp
erase tmp.dta

gen T = B/SE
gen signif = abs(T) >= 1.96 & !mi(T)

gen UB = B + 1.96*SE
gen LB = B - 1.96*SE

gen lN = log(N)

foreach i in 10 100 1000 {
	local lni = log(`i')
	local myxlab `myxlab' `lni' "`i'"
}	

#delimit;

gr tw
	
	(sc B lN if signif == 1 & !inlist(juris, "FW", "EBR"), 
		col(gray) msym(O))
	(sc B lN if signif == 0 & !inlist(juris, "FW", "EBR"), 
		col(gray) msym(Oh))
	(rspike UB LB lN if !inlist(juris, "FW", "EBR"), col(gray))
	(lfit B lN if !inlist(juris, "FW", "EBR"), col(black) lwid(medthick))
	(sc B lN if inlist(juris, "FW", "EBR"), msym(none) mlab(juris)
		mlabpos(0) mlabcol(black))
	,
		yline(0, lpat(dash))
		by(design, legend(off) note(""))
		subtitle(, fcol(none) lwid(none))
		ytitle("Point estimate {&plusminus} 2 std. errors")
		xtitle("Number of observations")
		ylab(, angle(horiz))
		plotregion(style(none))
		xlab(`myxlab')
		xscale(range(2 8))
		;

#delimit cr

gr export "_output/figure A22.pdf", replace

*******************************************************************************
* End
*******************************************************************************
